package com.controller;

import com.alibaba.fastjson.JSON;
import com.constant.Constant;
import com.entity.Chapter;
import com.github.pagehelper.PageInfo;
import com.service.ChapterService;
import com.util.AjaxResult;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * date:2021/5/7
 * description:
 * Author:Stargazer
 * /chapter/showChapterFront.do?course_id={{item.id}
 */
@Controller
@RequestMapping("/chapter")
public class ChapterController {
    @Autowired
    ChapterService chapterService;

    @RequestMapping("/showChapterFront.do")
    public String showChapterForFront(@RequestParam("course_id") Integer courseId, HttpSession session) {
        session.setAttribute("course_id",courseId);
        List<Chapter> chapters = chapterService.findChaptersByCourseId(courseId);
        session.setAttribute("chapters", chapters);
        return "front/front_course";
    }

    //    ===张
    @RequestMapping(value = "/loadChapter.do" ,produces = "text/plain;charset=utf-8")
    @ResponseBody
    //查询当前课程中的章节
    public String loadChapter(Integer pageNo, Chapter chapter, @DateTimeFormat(pattern="yyyy-MM-dd") Date begin_date, @DateTimeFormat(pattern="yyyy-MM-dd") Date end_date){
        System.out.println(chapter+"1010101"+begin_date);
        PageInfo<Chapter> info = new PageInfo<>();
        try {
            info = chapterService.loadChapters(pageNo,chapter,begin_date,end_date);
            return JSON.toJSONString(AjaxResult.isOk(info));
        }catch (Exception e){
            e.printStackTrace();
        }
        return  JSON.toJSONString(AjaxResult.isFail(info));
    }

    //添加章节
    @RequestMapping(value = "/addChapter.do")
    @ResponseBody
    public AjaxResult check(Chapter chapter) {
        chapter.setCreate_date(new Date());
        chapter.setStatus(Constant.STATUS_ENABLE);
        System.out.println("1212121000000000000000000000000");
        return chapterService.insertChapter(chapter);

    }

    //修改前查询章节信息
    @RequestMapping("/showToModify.do" )
    @ResponseBody
    public AjaxResult showToModify(Integer id){
        System.out.println("showToModify.do=====================");
        Chapter chapter = chapterService.findChapterById(id);
        System.out.println("showToModify==111111111111111111111111");
        AjaxResult ajaxResult = new AjaxResult(true,"",chapter);
        System.out.println("showToModify==22222222222");
        return  ajaxResult;
    }

    //修改章节信息}
    @RequestMapping("/modifyChapter.do")
    @ResponseBody
    public AjaxResult modifyChapter(String title,String info,Integer id){
        System.out.println("modifyChapter============00000000000");
        return chapterService.updateChapter(title,info,id);
    }

    @RequestMapping("/modifyChapter1.do")
    @ResponseBody
    //启用 禁用状态
    public AjaxResult modifyChapterStatus(Integer id,Integer status){
        System.out.println("modifyChapterStatus====================");
        boolean b = chapterService.updateChapterStatuss(id, status);

        return b ? new AjaxResult().isOk(""):new AjaxResult().isFail("");
    }

    //跳转到相关评论
    @RequestMapping("/sendChapterId.do")
    public String sendChapterId(){

        return "redirect:/back_cmm.do";
    }
    //    ===张

}
